package com.neusoft.neuiotms.dcom.converter.problem;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.common.model.Dictionary;
import com.neusoft.neuiotms.common.repository.DictionaryRepository;
import com.neusoft.neuiotms.dcom.dto.problem.ProblemRegistrationDTO;
import com.neusoft.neuiotms.dcom.model.problem.ProblemRegistration;
import com.neusoft.neuiotms.dcom.model.problem.ProblemRegistrationAttachment;
import com.neusoft.neuiotms.dcom.repository.problem.ProblemRegistrationRepository;

@Component
public class ProblemRegistrationConverter extends AbstractConverter<ProblemRegistration, ProblemRegistrationDTO> {

    @Autowired
    private ProblemRegistrationRepository problemRegistrationRepository;

    @Autowired
    private DictionaryRepository dictionaryRepository;

    @Autowired
    private ProblemRegistrationAttachmentConverter problemRegistrationAttachmentConverter;

    @Override
    public ProblemRegistration toModel(final ProblemRegistrationDTO dto) {
        ProblemRegistration model;
        if (dto.isNew()) {
            model = new ProblemRegistration();
        } else {
            model = this.problemRegistrationRepository.getOne(dto.getId());
        }

        model.setTaskId(dto.getTaskId());
        model.setWorkOrderNum(dto.getWorkOrderNum());
        model.setTitle(dto.getTitle());
        model.setCiCode(dto.getCiCode());
        model.setCiName(dto.getCiName());
        model.setCiId(dto.getCiId());

        model.setApplicationType(dto.getApplicationType());
        model.setApplicationTime(dto.getApplicationTime());
        model.setApplicant(dto.getApplicant());
        model.setContactInformation(dto.getContactInformation());

        model.setInfluenceCoverage(dto.getInfluenceCoverage());
        model.setUrgencyDegree(dto.getUrgencyDegree());
        model.setPriority(dto.getPriority());

        model.setDescription(dto.getDescription());

        model.setExecutorCode(dto.getExecutorCode());
        model.setExecutor(dto.getExecutor());

        model.setRegistrantCode(dto.getRegistrantCode());
        model.setRegistrant(dto.getRegistrant());

        model.setSource(dto.getSource());

        if ((dto.getAttachments() != null) && !dto.getAttachments().isEmpty()) {
            final List<ProblemRegistrationAttachment> list =
                    this.problemRegistrationAttachmentConverter.toListModel(dto.getAttachments());
            list.forEach(t -> t.setProblemRegistration(model));
            model.setAttachments(list);
        }
        model.setUpdateTime(new Date());
        return model;
    }

    @Override
    public ProblemRegistrationDTO toDTO(final ProblemRegistration model, final boolean forListView) {
        final ProblemRegistrationDTO dto = new ProblemRegistrationDTO();

        dto.setTaskId(model.getTaskId());
        dto.setWorkOrderNum(model.getWorkOrderNum());
        dto.setId(model.getId());
        dto.setTitle(model.getTitle());
        dto.setCiCode(model.getCiCode());
        dto.setCiName(model.getCiName());
        dto.setCiId(model.getCiId());

        dto.setApplicant(model.getApplicant());
        dto.setApplicationTime(model.getApplicationTime());
        dto.setApplicationType(model.getApplicationType());
        dto.setApplicationTypeDesc(this.getDictinonayText(model.getApplicationType()));
        dto.setContactInformation(model.getContactInformation());

        dto.setInfluenceCoverage(model.getInfluenceCoverage());
        dto.setInfluenceCoverageDesc(this.getDictinonayText(model.getInfluenceCoverage()));
        dto.setUrgencyDegree(model.getUrgencyDegree());
        dto.setUrgencyDegreeDesc(this.getDictinonayText(model.getUrgencyDegree()));
        dto.setPriority(model.getPriority());
        dto.setPriorityDesc(this.getDictinonayText(model.getPriority()));

        dto.setDescription(model.getDescription());

        dto.setExecutorCode(model.getExecutorCode());
        dto.setExecutor(model.getExecutor());

        dto.setRegistrantCode(model.getRegistrantCode());
        dto.setRegistrant(model.getRegistrant());

        dto.setSource(model.getSource());
        dto.setSourceDesc(this.getDictinonayText(model.getSource()));

        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        if (!forListView) {
            dto.setAttachments(this.problemRegistrationAttachmentConverter.toListDTO(model.getAttachments()));
        }
        return dto;
    }

    private String getDictinonayText(final String code) {
        if (code != null) {
            final Dictionary dic = this.dictionaryRepository.findByCode(code);
            if (dic != null) {
                return dic.getText();
            }
        }
        return "";
    }

}
